﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using CustomersSuppliersBusinessLayer;
using CustomersSuppliersDataLayer;
using System.Collections;
using WebHelper.Helpers;

namespace CustomersSuppliersWebApplication
{
    public partial class WebFormAddresses : WebFormBase
    {
        public Guid? Dealer_ID
        {
            get
            {
                return string.IsNullOrEmpty(this.Request[BLDealer.PropertiesNames.Dealer_ID]) ? (Guid?)null : new Guid(this.Request[BLDealer.PropertiesNames.Dealer_ID]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                InitializeTelerikRequest(this.RadTextBoxSearch, "onkeyup");
                BLDealer dealer = new BLDealer();
                dealer.Dealer_ID = this.Dealer_ID;
                dealer.LoadByIdentifiers();
                this.PageTitle ="عناوين /"+ dealer.Dealer_NameAr;
                this.getCountries();
            }
        }
        protected void RadTextBoxSearch_TextChanged(object sender, EventArgs e)
        {
            this.getAddress();
            this.RadGridResults.Rebind();
        }
        protected void RadComboBoxCountries_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.getAddress();
            this.RadGridResults.Rebind();
        }
        protected void RadGridResults_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.getAddress();
        }
        
        protected void RadGridResults_ItemCreated(object sender, GridItemEventArgs e)
        {
            //pager
            if (e.Item is GridPagerItem)
            {
                GridPagerItem item = e.Item as GridPagerItem;
                Button changePageSize = (Button)e.Item.FindControl("ChangePageSizeLinkButton");
                changePageSize.Text = "تغيير";
                Button goTO = (Button)e.Item.FindControl("GoToPageLinkButton");
                goTO.Text = "تغيير";
                Label itesNumberLabel = (Label)e.Item.FindControl("ChangePageSizeLabel");
                itesNumberLabel.Text = "          عدد الصفوف :";
                Label goTOLabel = (Label)e.Item.FindControl("GoToPageLabel");
                goTOLabel.Text = "صفحة :";
                Label ofLabel = (Label)e.Item.FindControl("PageOfLabel");
                ofLabel.Text = "من " + item.Paging.PageCount.ToString();
            }
        }
        protected void RadGridResults_ItemCommand(object sender, GridCommandEventArgs e)
        {
            //set default value
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                //e.Canceled = true;
                //System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
                //newValues[BLAddresse.PropertiesNames.Address_NameAr] = "***";
                //e.Item.OwnerTableView.InsertItem(newValues);
            }

        }
        protected void RadGridResults_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            BLAddresse Address = new BLAddresse();
            if (newValues[BLAddresse.PropertiesNames.Address_Code] != null)
            {
                Address.Address_Code = newValues[BLAddresse.PropertiesNames.Address_Code].ToString();
            }
            RadComboBox radComboBoxCountries = (RadComboBox)editedItem.FindControl("RadComboBoxCountries");
            if (radComboBoxCountries.SelectedValue != "")
            {
                Address.Country_ID = new Guid(radComboBoxCountries.SelectedValue);
            }
            if (newValues[BLAddresse.PropertiesNames.State_NameAr] != null)
            {
                Address.State_NameAr = newValues[BLAddresse.PropertiesNames.State_NameAr].ToString();
            }
            if (newValues[BLAddresse.PropertiesNames.State_NameEn] != null)
            {
                Address.State_NameEn = newValues[BLAddresse.PropertiesNames.State_NameEn].ToString();
            }
            if (newValues[BLAddresse.PropertiesNames.Town_NameAr] != null)
            {
                Address.Town_NameAr = newValues[BLAddresse.PropertiesNames.Town_NameAr].ToString();
            }
            if (newValues[BLAddresse.PropertiesNames.Town_NameEn] != null)
            {
                Address.Town_NameEn = newValues[BLAddresse.PropertiesNames.Town_NameEn].ToString();
            }
            if (newValues[BLAddresse.PropertiesNames.Address_NameAr] != null && newValues[BLAddresse.PropertiesNames.Address_NameAr].ToString().Trim() != "")
            {
                Address.Address_NameAr = newValues[BLAddresse.PropertiesNames.Address_NameAr].ToString();
            }
            else
            {
                this.ShowNotification("من فضلك قم بادخال العنوان", NotificationIconType.Warning);
                e.Canceled = true;
                return;
            }

            if (newValues[BLAddresse.PropertiesNames.Address_NameEn] != null)
            {
                Address.Address_NameEn = newValues[BLAddresse.PropertiesNames.Address_NameEn].ToString();
            }
            if (newValues[BLAddresse.PropertiesNames.Postal_Code] != null)
            {
                Address.Postal_Code = newValues[BLAddresse.PropertiesNames.Postal_Code].ToString();
            }
            if (newValues[BLAddresse.PropertiesNames.Address_Notes] != null)
            {
                Address.Address_Notes = newValues[BLAddresse.PropertiesNames.Address_Notes].ToString();
            }
            Address.Dealer_ID = this.Dealer_ID;
            Address.Address_Search = StringHelper.ToUniqueStatement(Address.Address_Code + Address.Address_NameAr + Address.Address_NameEn + Address.Address_Notes);
            Address.AutoSave();
            if (Address.Error == null)
            {
                this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void RadGridResults_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            if (e.Item.OwnerTableView.Name.ToLower() == "Addresss".ToLower())
            {
                BLAddresse Address = new BLAddresse();
                Address.Address_ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLAddresse.PropertiesNames.Address_ID].ToString());
                Address.LoadByIdentifiers();
                if (newValues[BLAddresse.PropertiesNames.Address_Code] != null)
                {
                    Address.Address_Code = newValues[BLAddresse.PropertiesNames.Address_Code].ToString();
                }
                RadComboBox radComboBoxCountries = (RadComboBox)editedItem.FindControl("RadComboBoxCountries");
                if (radComboBoxCountries.SelectedValue != "")
                {
                    Address.Country_ID = new Guid(radComboBoxCountries.SelectedValue);
                }
                if (newValues[BLAddresse.PropertiesNames.State_NameAr] != null)
                {
                    Address.State_NameAr = newValues[BLAddresse.PropertiesNames.State_NameAr].ToString();
                }
                if (newValues[BLAddresse.PropertiesNames.State_NameEn] != null)
                {
                    Address.State_NameEn = newValues[BLAddresse.PropertiesNames.State_NameEn].ToString();
                }
                if (newValues[BLAddresse.PropertiesNames.Town_NameAr] != null)
                {
                    Address.Town_NameAr = newValues[BLAddresse.PropertiesNames.Town_NameAr].ToString();
                }
                if (newValues[BLAddresse.PropertiesNames.Town_NameEn] != null)
                {
                    Address.Town_NameEn = newValues[BLAddresse.PropertiesNames.Town_NameEn].ToString();
                }
                if (newValues[BLAddresse.PropertiesNames.Address_NameAr] != null && newValues[BLAddresse.PropertiesNames.Address_NameAr].ToString().Trim() != "")
                {
                    Address.Address_NameAr = newValues[BLAddresse.PropertiesNames.Address_NameAr].ToString();
                }
                else
                {
                    this.ShowNotification("من فضلك قم بادخال الاسم", NotificationIconType.Warning);
                    e.Canceled = true;
                    return;
                }
                if (newValues[BLAddresse.PropertiesNames.Address_NameEn] != null)
                {
                    Address.Address_NameEn = newValues[BLAddresse.PropertiesNames.Address_NameEn].ToString();
                }
                if (newValues[BLAddresse.PropertiesNames.Postal_Code] != null)
                {
                    Address.Postal_Code = newValues[BLAddresse.PropertiesNames.Postal_Code].ToString();
                }
                if (newValues[BLAddresse.PropertiesNames.Address_Notes] != null)
                {
                    Address.Address_Notes = newValues[BLAddresse.PropertiesNames.Address_Notes].ToString();
                }
                Address.Dealer_ID = this.Dealer_ID;
                Address.Address_Search = StringHelper.ToUniqueStatement(Address.Address_Code + Address.Address_NameAr + Address.Address_NameEn + Address.Address_Notes);
                Address.AutoSave();
                if (Address.Error == null)
                {
                    this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
                }
                else
                {
                    this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                    e.Canceled = true;
                    return;
                }
            }
        }
        protected void RadGridResults_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            BLAddresse Address = new BLAddresse();
            Address.Address_ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLAddresse.PropertiesNames.Address_ID].ToString());
            Address.DeleteByIdentifiers();
            if (Address.Error == null)
            {
                this.ShowNotification("تم الحذف بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحذف", NotificationIconType.Error);
                return;
            }
        }
        protected void RadGridResults_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                //combobox Country
                RadComboBox radComboBoxCountries = (RadComboBox)e.Item.FindControl("RadComboBoxCountries");
                radComboBoxCountries.DataSource = CashVariables.CountryCollection;
                radComboBoxCountries.DataTextField = BLCountry.PropertiesNames.Country_NameAr;
                radComboBoxCountries.DataValueField = BLCountry.PropertiesNames.Country_ID;
                radComboBoxCountries.DataBind();
                if (e.Item.ItemIndex != -1)
                {
                    BLAddresse Address = (BLAddresse)e.Item.DataItem;
                    radComboBoxCountries.SelectedValue = Address.Country_ID.ToString();
                }
                
                //width
                GridEditableItem editedItem = e.Item as GridEditableItem;
                GridEditManager editMan = editedItem.EditManager;
                GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)(editMan.GetColumnEditor(BLAddresse.PropertiesNames.Address_Notes));
                editor.TextBoxMode = TextBoxMode.MultiLine;
                editor.TextBoxControl.Width = Unit.Pixel(300);
                editor.TextBoxControl.Height = Unit.Pixel(40);
            }
        }


        private void getAddress()
        {
            BLAddresseCollection AddressCollection = new BLAddresseCollection();
            //sort
            AddressCollection.Template.AddSort(BLCountry.PropertiesNames.Country_NameAr, false);
            AddressCollection.Template.AddSort(BLAddresse.PropertiesNames.State_NameAr, false);
            AddressCollection.Template.AddSort(BLAddresse.PropertiesNames.Town_NameAr, false);
            AddressCollection.Template.AddSort(BLAddresse.PropertiesNames.Address_NameAr, false);
            
            //Address
            AddressCollection.Template.AddFilter(LogicalOperators.And, BLAddresse.PropertiesNames.Dealer_ID, FilterOperators.Equal, this.Dealer_ID, typeof(Guid));

            //Country filter
            if (this.RadComboBoxCountries.SelectedValue == "11111111-1111-1111-1111-111111111111")
            {

            }
            else if (this.RadComboBoxCountries.SelectedValue == "00000000-0000-0000-0000-000000000000")
            {
                AddressCollection.Template.AddFilter(LogicalOperators.And, BLAddresse.PropertiesNames.Country_ID, FilterOperators.Undefined, null, typeof(Guid));
            }
            else
            {
                AddressCollection.Template.AddFilter(LogicalOperators.And, BLAddresse.PropertiesNames.Country_ID, FilterOperators.Equal, new Guid(this.RadComboBoxCountries.SelectedValue), typeof(Guid));
            }

            //Addressearch filter
            AddressCollection.Template.AddFilter(LogicalOperators.And, BLAddresse.PropertiesNames.Address_Search, FilterOperators.Contains, this.RadTextBoxSearch.Text, typeof(string));
            
            //load
            AddressCollection.Load();
            for (int i = 0; i < AddressCollection.Count; i++)
            {
                int index = CashVariables.CountryCollection.FindByIentifier(AddressCollection[i].Country_ID);
                if (index != -1)
                {
                    AddressCollection[i].Country_NameAr = CashVariables.CountryCollection[index].Country_NameAr;
                }
            }
            
            if (AddressCollection.Error != null)
            {
                this.ShowNotification("حدث خطا اثناء استعراض البيانات", NotificationIconType.Error);
            }
            this.RadGridResults.DataSource = AddressCollection;
            this.RadGridResults.MasterTableView.DataKeyNames = BLAddresse.PropertiesNames.Identifiers;
        }
        private void getCountries()
        {
            BLCountryCollection CountryCollection = new BLCountryCollection(CashVariables.CountryCollection);
            CountryCollection.Insert(0, new BLCountry { Country_ID = new Guid("00000000-0000-0000-0000-000000000000"), Country_NameAr = "بدون" });
            CountryCollection.Insert(0, new BLCountry { Country_ID = new Guid("11111111-1111-1111-1111-111111111111"), Country_NameAr = "الكل" });
            this.RadComboBoxCountries.DataSource = CountryCollection;
            this.RadComboBoxCountries.DataTextField = BLCountry.PropertiesNames.Country_NameAr;
            this.RadComboBoxCountries.DataValueField = BLCountry.PropertiesNames.Country_ID;
            this.RadComboBoxCountries.DataBind();
        }

    }
}